//
//  LoginInPutView.m
//  HaiGeEr_Student
//
//  Created by liuchen on 2018/9/9.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "LoginInPutView.h"
@interface LoginInPutView()<UITextFieldDelegate>
@property (nonatomic,weak) UIImageView *iconImg;
@property (nonatomic,weak) UILabel *lineLabel;
@property (nonatomic,weak) UIButton *deleteBtn;
@end

@implementation LoginInPutView

- (instancetype)initWithFrame:(CGRect)frame WithImageName:(NSString *)imageName WithTitle:(NSString *)title WithLocation:(NSInteger)location{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViewWithFrame:frame WithImageName:imageName WithTitle:title WithLocation:location];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubViewWithFrame:(CGRect)frame WithImageName:(NSString *)imageName WithTitle:(NSString *)title WithLocation:(NSInteger)location{//615*75
    //视图
    UIImageView *iconImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    _iconImg = iconImg;
    [self addSubview:self.iconImg];
    UITextField *textField = [[UITextField alloc]init];
    textField.textAlignment = NSTextAlignmentLeft;
    textField.font = [UIFont systemFontOfSize:LoginFont-3];
    textField.textColor = LOGINTITLERGBA;
    textField.placeholder = title;
    [textField setClearsOnBeginEditing:NO];
    [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
    _textField = textField;
    self.textField.delegate = self;
    [self addSubview:self.textField];
    UILabel *lineLabel = [[UILabel alloc]init];
    lineLabel.backgroundColor = LOGINBACKRGBA;
    _lineLabel = lineLabel;
    [self addSubview:self.lineLabel];
    
    //视图
    WS(ws);
    float widthToLeft=15*SCALEUNIT;
    [self.iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws).with.offset(widthToLeft);
        make.centerY.mas_equalTo(ws);
        make.size.mas_equalTo(CGSizeMake(32*SCALEUNIT, 32*SCALEUNIT));
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.iconImg.mas_right).with.offset(20*SCALEUNIT);
        make.centerY.mas_equalTo(ws.iconImg).with.offset(3*SCALEUNIT);
        make.right.mas_equalTo(ws.mas_right).with.offset(location==2?-154*SCALEUNIT:-24*SCALEUNIT);
        //make.size.mas_equalTo(CGSizeMake(400*SCALEUNIT, 40*SCALEUNIT));
    }];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(ws).with.offset(location==0?0:location==1?-40*SCALEUNIT:-150*SCALEUNIT);
        make.centerY.mas_equalTo(ws.textField);
        make.size.mas_equalTo(CGSizeMake(35*SCALEUNIT, 35*SCALEUNIT));
    }];
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(ws);
        make.height.mas_equalTo(2*SCALEUNIT);
    }];
}


#pragma mark ==========用户编辑时调用==========
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//    self.deleteBtn.hidden = NO;
//    self.deleteBtn.userInteractionEnabled = YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
//    self.deleteBtn.hidden = YES;
//    self.deleteBtn.userInteractionEnabled = NO;
}

#pragma mark ==========删除内容==========
- (void)deleteChange:(UIButton *)btn{
    self.textField.text = @"";
}

#pragma mark ==========禁止输入空格==========
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    if (![string isEqualToString:tem]) {
        return NO;
    }
    return YES;
}


@end
